package xxy.com.yangproject.database.db;

import android.content.Context;
import android.os.AsyncTask;


import androidx.annotation.NonNull;
import androidx.room.Dao;
import androidx.room.Database;
import androidx.room.DatabaseConfiguration;
import androidx.room.InvalidationTracker;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteOpenHelper;

import java.util.ArrayList;
import java.util.ListIterator;

import xxy.com.yangproject.database.Dao.CheeseDao;
import xxy.com.yangproject.database.Entity.Cheese;

@Database(entities = {Cheese.class}, version = 1, exportSchema = false)
public abstract  class CheeseDb extends RoomDatabase {
    //cheesedao里面有增删改查
    public abstract CheeseDao cheeseDao();

    public static final String DATABASE_NAME = "cheese.db";

    private static CheeseDb INSTANCE;

    public static CheeseDb getDatabase(final Context context){
        if (INSTANCE != null) {
            synchronized (CheeseDb.class){
                if (INSTANCE != null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            CheeseDb.class, DATABASE_NAME)
                            .fallbackToDestructiveMigration()    //Migration 迁移
                            .addCallback(callback)
                            .build();
                }
            }
        }
        return INSTANCE;
    }

    private static RoomDatabase.Callback callback  = new RoomDatabase.Callback(){
        @Override
        public void onOpen(@NonNull SupportSQLiteDatabase db) {
            super.onOpen(db);
            new PopulateDbAsync(INSTANCE).execute();  //执行AsyncTask
        }
    };

    private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {

        private final CheeseDao cheeseDao;

        public PopulateDbAsync(CheeseDb cheeseDb) {
            cheeseDao = cheeseDb.cheeseDao();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            ArrayList<Cheese> cheeses = new ArrayList<>();
            for (String name : data) {
                cheeses.add(new Cheese(name));
            }
            cheeseDao.insertList(cheeses);    //向DB中加入数据
            return null;
        }

        public static final String[] data = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
                "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale",
                "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
                "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh", "Anthoriro", "Appenzell",
                "Aragon", "Ardi Gasna", "Ardrahan", "Armenian String", "Aromes au Gene de Marc",
                "Asadero", "Asiago", "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss",
                "Babybel", "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal", "Banon",
                "Barry's Bay Cheddar", "Basing", "Basket Cheese", "Bath Cheese", "Bavarian Bergkase",
                "Baylough", "Beaufort", "Beauvoorde", "Beenleigh Blue", "Beer Cheese", "Bel Paese",
                "Bergader", "Bergere Bleue", "Berkswell", "Beyaz Peynir", "Bierkase", "Bishop Kennedy",
                "Blarney", "Bleu d'Auvergne", "Bleu de Gex", "Bleu de Laqueuille",
                "Bleu de Septmoncel", "Bleu Des Causses", "Blue", "Blue Castello", "Blue Rathgore",
                "Blue Vein (Australian)", "Blue Vein Cheeses", "Bocconcini", "Bocconcini (Australian)",
                "Boeren Leidenkaas", "Bonchester", "Bosworth", "Bougon", "Boule Du Roves",
                "Boulette d'Avesnes", "Boursault", "Boursin", "Bouyssou", "Bra", "Braudostur",
                "Breakfast Cheese", "Brebis du Lavort", "Brebis du Lochois", "Brebis du Puyfaucon",
                "Bresse Bleu", "Brick", "Brie", "Brie de Meaux", "Brie de Melun", "Brillat-Savarin",
                "Brin", "Brin d' Amour", "Brin d'Amour", "Brinza (Burduf Brinza)",
                "Briquette de Brebis", "Briquette du Forez", "Broccio", "Broccio Demi-Affine",
                "Brousse du Rove", "Bruder Basil", "Brusselae Kaas (Fromage de Bruxelles)", "Bryndza",
                "Buchette d'Anjou", "Buffalo", "Burgos", "Butte", "Butterkase", "Button (Innes)",
                "Buxton Blue", "Cabecou", "Caboc"};
    }




    @NonNull
    @Override
    protected SupportSQLiteOpenHelper createOpenHelper(DatabaseConfiguration config) {
        return null;
    }

    @NonNull
    @Override
    protected InvalidationTracker createInvalidationTracker() {
        return null;
    }

    @Override
    public void clearAllTables() {

    }
}
